function deleteEntity(id, target) {
    if (!confirm('是否删除' + target)) return
    $.ajax({
        url: 'api/' + target + '/' + id,
        type: 'delete',
        success: function (result) {
            if (result['success']) location.reload()
            else alert('删除' + target + '失败')
        }
    })
}

function openPanel(id, target) {
    var panel = document.getElementById(target + '-panel');
    panel.style.display = 'flex'
    var entity = {}
    $.ajax({
        url: 'api/' + target + '/' + id,
        type: 'get',
        success: function (result) {
            entity = result
            for (var x in entity) {
                var element = document.getElementById(target + '-' + x.toString());
                if (element !== null) element.value = entity[x]
            }
        }
    })
    document.getElementById(target + '-button').onclick = () => {
        for (var x in entity) {
            var element = document.getElementById(target + '-' + x.toString())
            if (element !== null) {
                if (element.type === 'number') entity[x] = parseInt(element.value)
                else entity[x] = element.value
            }
        }
        if (entity['start'] !== null && entity['end'] !== null) {
            if (entity['start'] > entity['end']) {
                alert("开始时间不能晚于结束时间")
                return
            }
        }
        $.ajax({
            url: 'api/' + target,
            type: 'put',
            data: JSON.stringify(entity),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function (result) {
                if (result['success']) location.reload()
                else alert('修改' + target + '失败')
            }
        })
    }
    window.addEventListener('mousedown', (event) => {
        if (event.target.id === target + '-panel')
            event.target.style.display = 'none'
    })
}